<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

$uid = $_G['uid'];

// 插件卡密
$table_er_kami = C::t('#erling_kami_member#er_kami_member');

//处理表单提交
if(submitcheck('submit')) {
    if(!submitcheck('submit', 1)) {
        showmessage('表单验证失败，请刷新后重试', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 验证验证码
    $verify_code = trim($_POST['seccode']);
    if(!$verify_code) {
        showmessage('请输入验证码', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    if(!check_seccode($verify_code, $_G['seccodedata']['code'])) {
        showmessage('验证码错误', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 检查用户是否登录
    if(!$uid) {
        showmessage('请先登录', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    
    $kami_code = trim($_POST['kami_code']);
    
    // 验证卡密
    $kami_info = $table_er_kami->fetch_by_code($kami_code);
    if(!$kami_info) {
        showmessage('卡密错误', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 检查卡密是否失效
    if($kami_info['status'] != 1) {
        showmessage('该卡密已失效', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    
    // 检查卡密是否已使用
    if($kami_info['used_uid']) {
        showmessage('该卡密已被使用', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    if(!function_exists('groupexpiry')) {
        function groupexpiry($groupterms) {
            $groupexpiry = 0;
            if(!empty($groupterms['main']['time'])) {
                $groupexpiry = $groupterms['main']['time'];
            }
            if(!empty($groupterms['ext'])) {
                foreach($groupterms['ext'] as $time) {
                    if($time > $groupexpiry) {
                        $groupexpiry = $time;
                    }
                }
            }
            return $groupexpiry;
        }
    }
    // 获取值
    $user_group = $kami_info['user_group'];//用户组id
    $user_group_days = $kami_info['user_group_days'] * 86400 + $_G['timestamp'];

    // 更新用户组
    // 1. 获取当前用户组信息
    $member = C::t('common_member')->fetch($uid);
    $memberfieldforum = C::t('common_member_field_forum')->fetch($uid);
    $groupterms = dunserialize($memberfieldforum['groupterms']);

    $extgroupids = $member['extgroupids'] ? explode("\t", $member['extgroupids']) : array();

    // 检查是否已有该用户组且是永久的
    $is_permanent = false;
    if(in_array($user_group, $extgroupids)) {
        $groupterms = dunserialize($memberfieldforum['groupterms']);
        if(empty($groupterms['ext'][$user_group])) {
            $is_permanent = true;
        }
    }
    
    if($is_permanent) {
        showmessage('您已拥有该用户组的永久权限，无需重复兑换', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    // 2. 处理扩展用户组 
    if(!in_array($user_group, $extgroupids)) {
        $extgroupids[] = $user_group;
    }
    $extgroupidsnew = implode("\t", array_filter($extgroupids));
    
    // 3. 更新用户组有效期
    if(isset($groupterms['ext'][$user_group]) && $groupterms['ext'][$user_group] > $_G['timestamp']) {
        $groupterms['ext'][$user_group] += $kami_info['user_group_days'] * 86400;
    } else {
        $groupterms['ext'][$user_group] = $user_group_days;
    }
    $groupexpirynew = groupexpiry($groupterms);
    
    // 4. 执行数据库更新
    C::t('common_member')->update($uid, array(
        'extgroupids' => $extgroupidsnew,
        'groupexpiry' => $groupexpirynew
    ));
    C::t('common_member_field_forum')->update($uid, array(
        'groupterms' => serialize($groupterms)
    ));

    // 更新卡密状态
    $kami_data = array(
        'uid' => $uid,
        'status' => 2,
        'used_time' => $_G['timestamp'],
    );
    $table_er_kami->update($kami_info['id'], $kami_data);

    // 跳转
    showmessage('兑换成功', 'home.php?mod=spacecp&ac=usergroup&gid='.$user_group, array(), array('showdialog' => true, 'locationtime' => 3));
    exit;
}